import { ChangeDetectionStrategy, Component } from '@angular/core';

@Component({
  templateUrl: './tab-bar-extra.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TabBarExtraComponent {
  index = 0;
  tabs = Array.from({ length: 15 }).map((_, index) => ({ name: `Tab ${index}`, id: index }));

  addTab(): void {
    const id = Number((Math.random() * 1000).toFixed(0));
    this.tabs.push({
      name: `Tab ${this.tabs.length + 1}`,
      id,
    });
    this.index = id;
  }

  removeTab(tab): void {
    const removeIndex = this.tabs.findIndex(item => tab.lvId === item.id);
    if (this.index === tab.lvId) {
      const nextIndex = removeIndex === this.tabs.length - 1 ? removeIndex - 1 : removeIndex + 1;
      this.index = this.tabs[nextIndex]?.id;
    }
    this.tabs.splice(removeIndex, 1);
  }
}
